package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import Main.SpaceGame;
import Tasks.MineAsteroids;

public class AsteroidMiner extends Construction
{
	boolean active = false;
	int x,y;
	BufferedImage sprite1 = null, sprite2 = null;
	SpaceGame main;
	int animateCnt;
	int currentAnimation;

	public AsteroidMiner(int x,int y,SpaceGame main)
	{
		super(x,y,main);
		
		height = 2;width = 2;
		this.main = main;
		this.x = x;
		this.y = y;
		animateCnt = 15;
		currentAnimation = 1;
		try {
			sprite1 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"AsteroidMiner1.png"));
			sprite2 = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"AsteroidMiner2.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		workingTile = main.world.tiles[x+1][y+1];
		
		setText();
	}

	@Override
	public void draw(Graphics g) 
	{
		if(animateCnt > 0 && !main.paused() && powered && active)
		{
			animateCnt--;
		}
		else
		{
			animateCnt = 15;
			if(currentAnimation == 1)
				currentAnimation = 2;
			else
				currentAnimation = 1;
		}
		if(currentAnimation == 1)
			g.drawImage(sprite1,x*10,y*10,null);
		else
			g.drawImage(sprite2,x*10,y*10,null);
		drawFlash(g,x,y);
	}
	
	public String getName()
	{
		return "Asteroid Miner";
	}
	
	@Override
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add(getName());
		
		if(powered)
		{
			colors.add(Color.cyan);
			colors.add(Color.cyan);
			text.add("");
			text.add("");
			
			if(!active)
			{
				colors.add(Color.green);
				colors.add(Color.cyan);
				text.add("m");
				text.add(" - Mine Asteroids");
			}
			else
			{
				colors.add(Color.red);
				colors.add(Color.cyan);
				text.add("m");
				text.add(" - Stop Mining");
			}
		}
		
		else
		{
			colors.add(Color.cyan);
			colors.add(Color.cyan);
			text.add("");
			text.add("");
			
			colors.add(Color.red);
			colors.add(Color.red);
			text.add("Needs Power");
			text.add("");
		}
	}
	
	public void update()
	{
		checkPowered(x,y);
		if(active)
		{
			if(Math.random() > .999)
			{
				main.resources.setMetal(main.resources.getMetal() + (int)(Math.random()*50));
			}
		}
	}
	
	public boolean isActive()
	{
		return active;
	}
	
	public void setActive(boolean a)
	{
		this.active = a;
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_M)
		{
			if (!active)
			{
				main.taskManager.taskList.add(new MineAsteroids(this));
			}
			else
				active = false;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}
}
